using System.Linq;
using RomanJendrusz.Amazon.ETL.Domain;
using System;


namespace RomanJendrusz.Amazon.ETL.Repositories
{
    public class SellerOffersRepository: Repository<SellerOffer, int>, ISellerOffersRepository
    {
        private readonly ISellersRepository sellerRepository;

        public SellerOffersRepository(ISession session, ISellersRepository sellerRepository)
            : base(session, s => session.SellerOffers, "SellerOfferId")
        {
            this.sellerRepository = sellerRepository;
        }

        

        public SellerOffer AttachIfAllreadyExists(SellerOffer item)
        {
            if(item  == null)
            {
                return null; 
            }
            var persistedOffer = session.SellerOffers
                .SingleOrDefault(so => so.SellerOfferId == item.SellerOfferId || so.ExchangeId == item.ExchangeId);
            if(persistedOffer == null)
            {
                item.Seller = sellerRepository.AttachIfAllreadyExists(item.Seller); 
                return item;
            }
            clone(item, persistedOffer);
            persistedOffer.Seller = sellerRepository.AttachIfAllreadyExists(item.Seller);
            return persistedOffer;
        }

        private void clone(SellerOffer attached, SellerOffer orginal)
        {
            if(attached.SellerOfferId != 0 && attached.SellerOfferId != orginal.SellerOfferId)
            {
                throw new ArgumentException("the orginal offer has a diffrent id the the attached");
            }
            if(attached.Book == null )
            {
                attached.Book = orginal.Book;
            }
            else if(orginal.Book == null)
            {
                //Leave it as it is
            }
            else
            {
                if (attached.Book.Asin != orginal.Book.Asin)
                {
                    throw new ArgumentException("The orginal offer has a diffrent book then the attached");
                }   
            }
            if(string.IsNullOrEmpty(attached.CoditionNote) == false)
            {
                orginal.CoditionNote = attached.CoditionNote;
            }
            if (string.IsNullOrEmpty(attached.Condition) == false)
            {
                orginal.Condition = attached.Condition;
            }
            if (attached.Currency != null)
            {
                orginal.Currency = attached.Currency;
            }
            if (attached.EndDate> orginal.EndDate)
            {
                orginal.EndDate = attached.EndDate;
            }
            if (attached.StartDate > orginal.StartDate)
            {
                orginal.StartDate = attached.StartDate;
            }
            if (attached.Price > 0)
            {
                orginal.Price = attached.Price;
            }
            if (attached.Quantity > 0)
            {
                orginal.Quantity = attached.Quantity;
            }
        }
    }
}